package com.liuzhou.action;

import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.liuzhou.util.AuthCodeUtil;

public class GetAuthCodeAction extends HttpServlet{
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//调用了验证码生成
		System.out.println("调用了验证码生成");		
		//生成正确答案
		System.out.println("验证码生成类又接收一次请求，随机数= "+req.getParameter("code"));
		
		String answer = AuthCodeUtil.getAuthCode();		
		BufferedImage img = AuthCodeUtil.getAuthImg(answer);
		//将正确答案放入session
		req.getSession().setAttribute("authcode", answer);
		ServletOutputStream out = resp.getOutputStream();
		ImageIO.write(img, "png", out);
		
		System.out.println("验证码已经被发送到前端");
		out.close();
	}
}
